<?php

require_once "./config/DB.config.php";

interface DBManager {

	public function query( $query );
	public function count( $ressource );
	public function fetch( $ressource );
	public function getError();
	public function getInsertedId();

}

class DB {

	private static $singleton	= null;
	private static $allRights	= true;

	private $ressource	= null;
	private $lastQuery		= null;
	private $error			= null;

	public function __construct() {
	
		global $DB_CONFIG;
	
		if ( empty( self::$singleton ) ) {
			$DataBaseClass		= $DB_CONFIG['DataBaseClass'];
			self::$singleton	= eval( "return new " . $DataBaseClass . "();" );
		}
	
	}
	
	public function query( $query ) {
	
		if ( $this->ressource = self::$singleton->query( $query ) ) {
			$this->lastQuery	= $query;
			return true;
		} else {
			$this->error	= self::$singleton->getError();
			throw new Exception( "SQL request faild : " . $query . "\nError description : " . $this->error, 0 );
			return false;
		}
		
	}
	
	public function count() {
	
		return self::$singleton->count( $this->ressource );	

	}
	
	public function sql2array() {
	
		$result	= array();
		$data	= $this->ressource;
		while ( $row = self::$singleton->fetch( $data ) ) {
			$result[]	= $row;
		}
		return $result;
	
	}
	
	public function fetch() {
	
		return self::$singleton->fetch( $this->ressource );
	
	}
	
	public function getInsertedId() {
	
		return self::$singleton->getInsertedId();
	
	}

}


?>